//
//  EaseIMSessionManager.m
//  HDMainWindow
//
//  Created by apple on 2017/5/8.
//  Copyright © 2017年 com.devin.definesys. All rights reserved.
//

#import "EaseIMSessionManager.h"
#import "DMMessageTextModel.h"
#import "DMMessageImageModel.h"

@implementation EaseIMSessionManager

//获取会话列表
-(NSArray <DMConverstaionModel *> *) getConverstaionList {
    
    NSMutableArray *mutableArray = [NSMutableArray array];
    
    DMConverstaionModel *model = nil ;
//    [[NIMSDK sharedSDK].conversationManager searchMessages:<#(nonnull NIMSession *)#> option:<#(nonnull NIMMessageSearchOption *)#> result:<#^(NSError * _Nullable error, NSArray<NIMMessage *> * _Nullable messages)result#>]
    
    NSArray<NIMRecentSession *> *sessionArray = [[NIMSDK sharedSDK].conversationManager allRecentSessions];
    if (sessionArray) {
        for (NIMRecentSession *recentSession in sessionArray) {
            model = [[DMConverstaionModel alloc] init];
            model.conversationId = recentSession.session.sessionId ;
            switch (recentSession.session.sessionType) {
                case NIMSessionTypeP2P:
                    model.conversationType = kLLConversationTypeChat ;
                    break;
                case NIMSessionTypeTeam:
                    model.conversationType = kLLConversationTypeGroupChat ;
                    break;
                case NIMSessionTypeChatroom:
                    model.conversationType = kLLConversationTypeChatRoom ;
                    break;
                default:
                    break;
            }
            model.latestMessageTimestamp = recentSession.lastMessage.timestamp ;
            model.unreadMessageNumber = recentSession.unreadCount ;
//          此处可以用网络请求通讯录列表，替换头像和昵称到其中
            model.avatarImageURL = recentSession.localExt[@"avatarImageURL"];
            model.nickName = recentSession.localExt[@"nickName"];
            //此处草稿只保存在本地
            model.draft = recentSession.localExt[@"draft"];
            
            //将Message转成Model
            model.referenceMessageModel = [self getMessageModelWithMessage:recentSession.lastMessage] ;
        }
    }
    
    return [NSArray arrayWithArray:mutableArray];

}

-(DMMessageBaseModel *) getMessageModelWithMessage:(id) message {
    if (!message || ![message isKindOfClass:[NIMMessage class]]) {
        return nil ;
    }
    
    NIMMessage *nimMessage = message ;
    
    

    
    /**
     *  文本类型消息
        NIMMessageTypeText          = 0,
     *  图片类型消息
        NIMMessageTypeImage         = 1,
     *  声音类型消息
        NIMMessageTypeAudio         = 2,
     *  视频类型消息
        NIMMessageTypeVideo         = 3,
     *  位置类型消息
        NIMMessageTypeLocation      = 4,
     *  通知类型消息
        NIMMessageTypeNotification  = 5,
     *  文件类型消息
        NIMMessageTypeFile          = 6,
     *  提醒类型消息
        NIMMessageTypeTip           = 10,
     *  自定义类型消息
        NIMMessageTypeCustom        = 100
     */
    DMMessageBaseModel *model = nil;

    switch (nimMessage.messageType) {
        case NIMMessageTypeText:{
            model = [[DMMessageTextModel alloc] init];
            DMMessageTextModel *textModel = (DMMessageTextModel *)model ;
            textModel.attrText = [[NSAttributedString alloc] initWithString:model.text];
            break;
        }
        case NIMMessageTypeImage:{
            NIMImageObject *imageObject = nimMessage.messageObject ;
            
            model = [[DMMessageImageModel alloc] init];
            DMMessageImageModel *imageModel = (DMMessageImageModel *)model ;
            imageModel.thumbnailImagePath = imageObject.thumbPath ;
            imageModel.fileRemotePath = imageObject.url ;
            imageModel.fileLocalPath = imageObject.path ;
            imageModel.fileSize = imageObject.fileLength ;
            imageModel.thumbnailImageSize = imageObject.size ;
            
            break;
        }
        case NIMMessageTypeAudio:
            break;
        case NIMMessageTypeVideo:
            break;
        case NIMMessageTypeLocation:
            break;
        case NIMMessageTypeNotification:
            break;
        case NIMMessageTypeFile:
            break;
        case NIMMessageTypeTip:
            break;
        case NIMMessageTypeCustom:
            break;
        default:
            break;
    }
    
    if (model) {
        model.messageId = nimMessage.messageId ;
        model.conversationId = nimMessage.session.sessionId ;
        model.from = nimMessage.from ;
        model.fromMe = nimMessage.isOutgoingMsg ;
        model.timestamp = nimMessage.timestamp ;
        model.text = nimMessage.text ;
        model.ext = nimMessage.remoteExt ;
    }
 
    
    return model ;
}

@end
